var {
  clickUtils,
  swipeUtils,
  timeUtils,
  randomUtils,
  ocrUtils,
  logUtils,
} = require("../../utils/index");
var Action = require("../../base/Action");
var aRead = new Action("阅读", {});
aRead.exec = function () {
  var contents = ["分钟前", "小时前", "天前", "周前", "前天", "月前"];
  var results = ocrUtils.scrollAndFindSome(contents);
  if (results.length > 0) {
    var stopIndex = 1;
    if (randomUtils.possible(90, 100)) {
      stopIndex = 2;
    }
    logUtils.info(
      "找到",
      results.length,
      "个阅读内容",
      "最多阅读",
      stopIndex,
      "个"
    );
    for (var index = 0; index < results.length; index++) {
      var TextOCR = results[index];
      clickUtils.clickTextOCR(TextOCR);
      sleep(randomUtils.getRandom(1.5, 2, 1000));
      this.startReadOrWatch();
      if (index == stopIndex) {
        break;
      }
    }
  } else {
    logUtils.info(this.name, "没找到要阅读的内容");
  }
};
aRead.startReadOrWatch = function () {
  var contents = ["全屏", "连播", "我的金币"];
  if (ocrUtils.captureExistsOneText(contents, true)) {
    this.startWatch();
  } else {
    this.startRead();
  }
};
aRead.startRead = function () {
  var span = randomUtils.getRandom(35, 60, 1);
  swipeUtils.swipeRead(span);
  back();
  sleep(randomUtils.getRandom(2, 3, 1000));
};
aRead.startWatch = function () {
  var startTime = Date.now();
  var span = randomUtils.getRandom(35, 120, 1);
  var count = 0;
  sleep(randomUtils.getRandom(10, 30, 1000));
  while (true) {
    count++;
    var hasExec = timeUtils.hasExec(startTime);
    logUtils.info("已经看视频", hasExec, "/", span);

    if (hasExec >= span) {
      logUtils.info("将结束看视频");
      break;
    }
    logUtils.info("开始滑动，看下一个视频");
    swipeUtils.swipeVideo();
    sleep(randomUtils.getRandom(10, 30, 1000));
  }
  back();
  sleep(randomUtils.getRandom(2, 3, 1000));
};
module.exports = aRead;
